/*
 * @Author: wurangkun wurangkun@qq.com
 * @Date: 2023-04-04 19:14:15
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-04-05 14:29:53
 * @FilePath: \myblog_server\src\mysql\index.ts
 * @Description: sequelize
 */
const Sequelize = require("sequelize");
const db = {
  database: "project", // 使用哪个数据库
  username: "root", // 用户名
  password: "123456", // 口令
  host: "localhost", // 主机名
  port: 3306, // 端口号，MySQL默认3306
};
const sequelize = new Sequelize(db.database, db.username, db.password, {
  host: db.host,
  dialect: "mysql",
  operatorAliases: false,
  pool: {
    max: 5,
    min: 0,
    idle: 30000,
  },
  //解决中文输入问题
  define: {
    charset: "utf8",
    dialectOptions: {
      collate: "utf8_general_ci",
    },
  },
});

// 测试连接是否成功
sequelize
  .authenticate()
  .then(() => {
    console.log("Connection has been established successfully.");
  })
  .catch((err) => {
    console.log("Unable to connect to the database", err);
  });

module.exports = sequelize;
